#include "engine.h"
#include "render_sys.h"
#include "camera.h"
#include "city.h"
#include "gui/gui.h"
#include "gui/gui_controller.h"
#include <ctime>
#include <sstream>
#include "music_player.h"

//////////////////////////////////////////////////
//  Engine::Engine()
//////////////////////////////////////////////////
Engine::Engine() :
    render(NULL),
    city(NULL),
    running(false),
    window(NULL),
    gui(NULL)
{
    music = new MusicPlayer;
    music->play();
}

//////////////////////////////////////////////////
//  void Engine::init()
//////////////////////////////////////////////////
void Engine::init()
{
    // Initialize and Setup Render System
    render = new RenderSys;
    window = render->init();

    // Create city
    city = new City;

    // Gui
    gui = Gui::get();
    gui->init(window, render->getCamera(), city);

    if (window)
    {
        running = true;
    }

    // Register window's input with the global input namespace
    //Input::registerInput(&window->GetInput());

    loop();
}

//////////////////////////////////////////////////
//  void Engine::loop()
//////////////////////////////////////////////////
void Engine::loop()
{
    sf::Event event;
    sf::Clock timer;

    while (running)
    {
        sf::Time dt = timer.getElapsedTime();
        timer.restart();
        //printf("FPS: %f\n", 1/dt);
        while (window->pollEvent(event))
        {
            eventHandle(event);
        }

        render->render(gui, city);
        update(dt.asSeconds());
    }
}

//////////////////////////////////////////////////
//  void Engine::eventHandle(sf::Event event)
//////////////////////////////////////////////////
void Engine::eventHandle(sf::Event event)
{
    if (event.type == sf::Event::Closed)
    {
        running = false;
    }
    else if (event.type == sf::Event::Resized)
    {
        sf::View* v = render->getCamera()->getView();
        v->setSize(event.size.width, event.size.height);
    }
    else if (event.type == sf::Event::KeyPressed)
    {
        if (event.key.code == sf::Keyboard::F5)
        {
            takeScreenshot();
        }
        else if (event.key.code == sf::Keyboard::Left)
        {
            music->previousSong();
        }
        else if (event.key.code == sf::Keyboard::Right)
        {
            music->nextSong();
        }
    }

    gui->eventHandle(event);
    city->eventHandle(event);
    render->eventHandle(event);

}

//////////////////////////////////////////////////
//  void Engine::update(double dt)
//////////////////////////////////////////////////
void Engine::update(double dt)
{
    city->update(dt);
    gui->update(dt);
    render->update(dt);
}

//////////////////////////////////////////////////
//  void Engine::takeScreenshot()
//////////////////////////////////////////////////
void Engine::takeScreenshot()
{
    sf::Image screenshot = window->capture();
    time_t t = time(0);
    struct tm* now = localtime(&t);
    std::stringstream ss;
    ss << "screenshots\\" << now->tm_year + 1900 << "-" << now->tm_mon << "-" << now->tm_mday
       << "--" << now->tm_hour << "-" << now->tm_min << "-" << now->tm_sec << ".png";
    screenshot.saveToFile(ss.str());
}
